﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ZX.WebApp.WorkFlow
{
    public partial class AccreditWorkflowList : System.Web.UI.Page
    {
        public string AccreditToUserId = string.Empty;
        public string AccreditFromUserId = string.Empty;
        public int Status = 0;
        //调用流程服务接口实例
        public  CrmWorkFlowService.WorkFlowServiceClient workFlowServiceClient = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            Status = Convert.ToInt32(Request.QueryString["Status"]); //是否回收状态
            AccreditToUserId = Request.QueryString["AccreditToUserId"].ToString(); //被代理人
            AccreditFromUserId = Request.QueryString["AccreditFromUserId"].ToString(); //代理人
            workFlowServiceClient = new CrmWorkFlowService.WorkFlowServiceClient();  //实例化流程服务接口
            if (!IsPostBack)
            {
                Bind();
                DataBinds();
            }
        }
        public void Bind()
        {
           //流程类型帮绑定
           ddlCaption.DataSource = workFlowServiceClient.GetWorkFlowCaptions(AccreditToUserId, AccreditFromUserId);
           ddlCaption.DataValueField = "WFClassId";
           ddlCaption.DataTextField = "Caption";
           ddlCaption.DataBind();
           this.ddlCaption.Items.Insert(0, new ListItem("请选择", "")); 

           //流程名称
           ddlFlowCaption.DataSource = workFlowServiceClient.GetInstanceCaption(AccreditToUserId, AccreditFromUserId);
           ddlFlowCaption.DataValueField = "WorkFlowId";
           ddlFlowCaption.DataTextField = "FlowCaption";
           ddlFlowCaption.DataBind();
           this.ddlFlowCaption.Items.Insert(0, new ListItem("请选择", "")); 
        }

        public void DataBinds()
        {
            //数据源绑定
            gvAccredList.DataSource = workFlowServiceClient.GetAccreditUserList(AccreditToUserId, AccreditFromUserId);
            gvAccredList.DataBind();
        }
        /// <summary>
        /// 绑定后触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvAccredList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DateTime acceditStartTime = new DateTime();
            DateTime acceditEndTime = new DateTime();
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                 Label wfClassId = (Label)e.Row.FindControl("lblWFClassId");  //流程类型ID
                if (((Label)e.Row.FindControl("lblAcceditStartTime")).Text.Trim() != "")
                {
                    acceditStartTime = Convert.ToDateTime(((Label)e.Row.FindControl("lblAcceditStartTime")).Text.Trim());//开始时间
                }
                if (((Label)e.Row.FindControl("lblAcceditEndTime")).Text.Trim() != "")
                {
                    acceditEndTime = Convert.ToDateTime(((Label)e.Row.FindControl("lblAcceditEndTime")).Text.Trim());//结束时间
                }
                if (acceditStartTime != null && acceditEndTime != null)
                {
                    if (DateTime.Now > acceditStartTime && DateTime.Now < acceditEndTime)
                    {
                        e.Row.Cells[8].Text = "是";
                        //row.Cells[8].ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        e.Row.Cells[8].Text = "否";
                        e.Row.Cells[8].ForeColor = System.Drawing.Color.Red;
                    }
                }
                else
                {
                    e.Row.Cells[8].Text = "否";
                    e.Row.Cells[8].ForeColor = System.Drawing.Color.Red;
                }
                if (e.Row.RowIndex > 0)
                {
                    Label hfClassId = (Label)gvAccredList.Rows[e.Row.RowIndex - 1].FindControl("lblWFClassId");
                    if (hfClassId.Text == wfClassId.Text)
                    {
                        e.Row.Cells[0].Text = "";
                        // row.Cells[0].BorderColor = System.Drawing.Color.Red;
                    }
                }
                if (Status == 0)
                {
                    ((LinkButton)e.Row.FindControl("hlkTakeback")).Visible = false;
                    ((CheckBox)e.Row.FindControl("ckWorkFlow")).Visible = false;
                }
                else
                {
                    Label lblStatus = (Label)e.Row.FindControl("lblAccreditMatters");  //代理状态
                    if (lblStatus.Text == "0" || lblStatus.Text == "1")
                    {
                        ((CheckBox)e.Row.FindControl("ckWorkFlow")).Checked = false;
                    }
                    else if (lblStatus.Text == "2")
                    {
                        ((CheckBox)e.Row.FindControl("ckWorkFlow")).Checked = true;
                    }

                }
                
            }

        }

    
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string str = string.Empty;
            if (ddlCaption.SelectedItem.Value != "")
            {
                if (str != "")
                {
                    str += " and WFClassId = '" + ddlCaption.SelectedItem.Value + "' and AccreditToUserId = '" + AccreditToUserId + "' and AccreditFromUserId = '" + AccreditFromUserId + "'";
                }
                else
                {
                    str = " WFClassId = '" + ddlCaption.SelectedItem.Value + "'and AccreditToUserId = '" + AccreditToUserId + "' and AccreditFromUserId = '" + AccreditFromUserId + "'";
                }

            }
            if (ddlFlowCaption.SelectedItem.Value != "")
            {
                if (str != "")
                {
                    str += " and WorkFlowId = '" + ddlFlowCaption.SelectedItem.Value + "'and AccreditToUserId = '" + AccreditToUserId + "' and AccreditFromUserId = '" + AccreditFromUserId + "'";
                }
                else
                {
                    str = " WorkFlowId = '" + ddlFlowCaption.SelectedItem.Value + "'and AccreditToUserId = '" + AccreditToUserId + "' and AccreditFromUserId = '" + AccreditFromUserId + "'";
                }
            }
            if (str == "")
            {
                str = " AccreditToUserId = '" + AccreditToUserId + "' and AccreditFromUserId = '" + AccreditFromUserId + "'";
            }
            gvAccredList.DataSource = workFlowServiceClient.GetAccreditList(str);
            gvAccredList.DataBind();
        }

        /// <summary>
        /// 是否收回流转中流程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void hlkTakeback_Click(object sender, EventArgs e)
        {
            GridViewRow row = (sender as LinkButton).Parent.Parent as GridViewRow;
            string accreditId = ((Label)row.FindControl("lblAccreditId")).Text;
            bool reust = false;
            if (((CheckBox)row.FindControl("ckWorkFlow")).Checked == true)
            {
                reust = workFlowServiceClient.RemoveMoveAccredit(accreditId, true, DateTime.Now);
            }
            else 
            {
                reust = workFlowServiceClient.RemoveMoveAccredit(accreditId, false, DateTime.Now);
            }
            if (reust)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Save", "<script>alert('收回代理成功')</script>");
            }
            else
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Save", "<script>alert('收回代理失败')</script>");
            }
            Bind();
            DataBinds();
         }
     
    }
}